﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TWS.Cart.Business;
using TWS.Cart.Data;

public partial class Application_Cart_Administration_ProductForm : System.Web.UI.Page
{
	int updateId = -1;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
			updateId = Convert.ToInt32(Request.QueryString["Id"]);

		if (!IsPostBack)
		{

			FillDDLS();


			//IF edit load data.
			if (updateId != -1)
			{
				CartCatProduct ssProduct = CartCatProduct.FetchByID(updateId);

				DropDownListCategoryId.SelectedValue = ssProduct.CategoriesId.ToString();
				TextBoxName.Text = ssProduct.Name;
				TextBoxSKU.Text = ssProduct.Sku;
				TextBoxDescription.Text = ssProduct.DescriptionShort;
				TextBoxQuantityInStock.Text = ssProduct.QuantityInStock.ToString();

				TextBoxWeight.Text = ssProduct.Weight.ToString();
				DropDownListWeightUnit.SelectedValue = ssProduct.WeightMeasurementUnitId.ToString();

				TextBoxWidth.Text = ssProduct.SizeWidth.ToString();
				TextBoxHeight.Text = ssProduct.SizeHeight.ToString();
				DropDownListSizeUnit.SelectedValue = ssProduct.SizeMeasurementUnitId.ToString();

				TextBoxPrice.Text = ssProduct.Price.ToString();
				TextBoxAmountPKG.Text = ssProduct.AmountPerPackage.ToString();
				
			}

		}
	}

	protected void FillDDLS()
	{
		//Construct DDLS

		//Category
		CartCatCategoryCollection cat = new CartCatCategoryCollection()
		.OrderByAsc(CartCatCategory.Columns.Name)
		.Load();

		DropDownListCategoryId.Items.Add(new ListItem("-Select-", ""));

		foreach (var c in cat)
			DropDownListCategoryId.Items.Add(new ListItem(c.Name, c.Id.ToString()));

		//
		CartCatMeasurementUnitCollection ssMUnit = new CartCatMeasurementUnitCollection()
		.Load();

		DropDownListSizeUnit.Items.Add(new ListItem("-Select-", ""));
		DropDownListWeightUnit.Items.Add(new ListItem("-Select-", ""));

		foreach (var mu in ssMUnit)
		{
			DropDownListSizeUnit.Items.Add(new ListItem(mu.Name, mu.Id.ToString()));
			DropDownListWeightUnit.Items.Add(new ListItem(mu.Name, mu.Id.ToString()));
		}

	}

	protected void ButtonNew_Click(object sender, EventArgs e)
	{
		CartCatProduct p;

		//Select if this is an update or a new register.
		if (updateId == -1)//Its a new record
			p = new CartCatProduct();
		else//We are updating
			p = CartCatProduct.FetchByID(updateId);

		p.CategoriesId = Convert.ToInt32(DropDownListCategoryId.SelectedValue);
		p.Name = TextBoxName.Text;
		p.Sku = TextBoxSKU.Text;
		p.DescriptionShort = TextBoxDescription.Text;
		if (!string.IsNullOrEmpty(TextBoxAmountPKG.Text))
			p.AmountPerPackage = Convert.ToInt32(TextBoxAmountPKG.Text);
		if (!string.IsNullOrEmpty(TextBoxQuantityInStock.Text))
			p.QuantityInStock = Convert.ToInt32(TextBoxQuantityInStock.Text);
		p.Price =Convert.ToDecimal(TextBoxPrice.Text);


		if (!string.IsNullOrEmpty(TextBoxWeight.Text))
		{
			p.Weight = Convert.ToDecimal(TextBoxWeight.Text);
			p.WeightMeasurementUnitId = Convert.ToInt32(DropDownListWeightUnit.SelectedValue);
		}

		if (!string.IsNullOrEmpty(TextBoxWidth.Text) && !string.IsNullOrEmpty(TextBoxHeight.Text))
		{
			p.SizeWidth = Convert.ToDecimal(TextBoxWidth.Text);
			p.SizeHeight = Convert.ToDecimal(TextBoxHeight.Text);
			p.SizeMeasurementUnitId = Convert.ToInt32(DropDownListSizeUnit.SelectedValue);
		}
		

		try
		{
			p.Save();

			FileUploader01.Save(p.Id);

			if (updateId == -1)
				MessageB01.Text = "Record inserted.";
			else
				MessageB01.Text = "Record updated.";

			MessageB01.Visible = true;
		}
		catch (Exception ex)
		{
			MessageB01.Text = "An Error Ocurred";
			MessageB01.Description = ex.Message +"<br>" + ex.StackTrace;
			MessageB01.Visible = true;
		}
	}
}
